﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectDeal.Models;

namespace ProjectDeal.Controllers
{
    public class DealController : Controller
    {
        //
        // GET: /Deal/
        db_Kayleehouse db = new db_Kayleehouse();
        public ActionResult DetailDeal(string ShortUrl)
        {
            try
            {
                if (ShortUrl.Equals("undefined"))
                {
                    return View((Deal)Session["Deal"]);
                }
                else
                {
                    var Deal = (from b in db.Deals
                                where b.URLRutGon.Equals(ShortUrl)
                                select b).Single();
                    Session["Deal"] = Deal;
                    return View(Deal);
                }
                
            }
            catch (Exception)
            {
                
                throw;
            }
           
            
        }

        public ActionResult DealTheoLoai(string ShortName,int page)
        {
            

            //Lay ma loai danh muc tu ten danh muc
            var obDeal = (from b in db.LoaiDeals
                         where b.ShortName == ShortName
                         select new{b.MaLoaiDeal,b.TenLoaiDeal}).Single();
            var Total = (from b in db.Deals
                         where b.MaLoaiDeal == obDeal.MaLoaiDeal
                         select b.MaDeal).Count();
            //Kiem tra xem co du lieu o danh muc nay khong
            var isAny = (from b in db.Deals
                         where b.MaLoaiDeal == obDeal.MaLoaiDeal
                         select b.MaDeal).Any();
            ViewBag.Title = obDeal.TenLoaiDeal;
            if (isAny)
            {
                var lsDeal = db.Deal_GetDealTheoDanhMuc(page, 9, 5, obDeal.MaLoaiDeal).ToList();
                ViewBag.PagingView = db.Deal_GetDealTheoDanhMucPhantrang(Total, page,
                    5, 9, ShortName).Single().ToString();
                
                return View(lsDeal);
            }
            else
            {
                var lsDeal = new List<Deal>();
                return View(lsDeal);
            }
            
        }
    }
}
